/**
 * com.yhte.mir.client.dao.impl
 * ClientDaoImpl.java
 * sunsu
 * 2012-9-18
 */
package com.yhte.mir.client.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.yhte.mir.client.dao.ClientDao;
import com.yhte.mir.ill.vo.IllInfo;

/**
 * @author sunsu
 *
 */
public class ClientDaoImpl extends SqlMapClientTemplate implements ClientDao {
	private static Logger log = Logger.getLogger(ClientDaoImpl.class.getName());
	
	@Override
	public void insert(String link,List<IllInfo> illInfos) {
		try {
			Connection conn = this.getSqlMapClient().getDataSource().getConnection();
			conn.setAutoCommit(false);
			
			String clientSql = "insert into t_blackcar@"+link+
					"(PLATE_NUM,PLATE_TYPE,UPDATE_TIME,STATE,REMARK,IMPORT_CAUSE,OWNER_NAME) " +
					" values(?,?,sysdate,'1',?,?,'CENTER')";
			String centerSql ="insert into center_hmd_vehicle(clbh,hphm,hpys,datalink) values (?,?,?,'"+link+"')";
			PreparedStatement clientPs = conn.prepareStatement(clientSql);
			PreparedStatement centerPs = conn.prepareStatement(centerSql);
			
			for(int i=0;i<illInfos.size();i++){
				IllInfo info = illInfos.get(i);
				clientPs.setString(1, info.getHphm());
				clientPs.setString(2, info.getHpys());
				clientPs.setString(3, info.getBz());
				clientPs.setString(4, info.getRktj());
				clientPs.addBatch();
				
				centerPs.setLong(1, info.getClbh());
				centerPs.setString(2, info.getHphm());
				centerPs.setString(3, info.getHpys());
				centerPs.addBatch();
				
				if(i!=0 && i%500==0){
					clientPs.executeBatch();
					centerPs.executeBatch();
					clientPs.clearBatch();
					centerPs.clearBatch();
					log.info("已同步车辆"+i+"辆");
				}
			}
			clientPs.executeBatch();
			centerPs.executeBatch();
			clientPs.clearBatch();
			centerPs.clearBatch();
			conn.commit();
			log.info("共同步车辆"+illInfos.size()+"辆");
		} catch (SQLException e) {
			log.error("同步前端查报站出错："+e.getMessage());
			e.printStackTrace();
		}
	}

}
